<?php
/**
 * Dependency img exporter
 *
 * PHP version 5
 *
 * @category  W
 * @package   Services
 * @author    Olivier Hoareau <olivier@phppro.fr>
 * @copyright 2009 PHPPRO http://www.phppro.fr
 * @license   http://dev.phppro.fr/license PHPPRO
 * @version   SVN: $Id:$
 * @link      http://dev.phppro.fr/projects/wdependency
 */
require_once 'W/Dependency/Exporter/Dot.php';
/**
 * Dependency png exporter
 *
 * @category  W
 * @package   Services
 * @author    Olivier Hoareau <olivier@phppro.fr>
 * @copyright 2009 PHPPRO http://www.phppro.fr
 * @license   http://dev.phppro.fr/license PHPPRO
 * @version   SVN: $Id$
 * @link      http://dev.phppro.fr/projects/wdependency
 */
abstract class W_Dependency_Exporter_Img extends W_Dependency_Exporter_Dot
{
    public function getFormat()
    {
        $pos = strrpos($this->getOutputFile(),'.');

        if (false === $pos) {
            throw new RuntimeException("Output file must have an extension (format detected based on the extension)");
        }
        return substr($this->getOutputFile(),$pos+1);
    }
    /**
	 * Exports the dependencies
	 *
	 * @param array $data the dependencies
	 *
	 * @return W_Dependency_Exporter_Interface
	 *
	 * @throws RuntimeException if an error occured
     */
    public function export($data)
    {
        $outputFile = $this->getOutputFile();
        if (null === $outputFile) {
            $outputFile = $this->getKey().'.'.$this->getFormat();
        }
        $pos = strrpos($outputFile,'.');
        if (false === $pos) {
            throw new RuntimeException("Output file must have an extension (format detected based on the extension)");
        }
        $dotFile = substr($outputFile,0,$pos).'.dot';
        $this->setOutputFile($dotFile);
        parent::export($data);
        $output = null;
        $return = 0;
        W_W::sys()->exec("dot -T".$this->getFormat()." -o\"$outputFile\" -Kdot < $dotFile",$output,$return);
        W_W::fs()->deleteFile($dotFile);
        return $this;
    }
}